﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class CountyController : Controller
    {
        //
        // GET: /County/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// get all county 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCounty()
        {
            string searchCounty = Request.Params["query"];

            CountyContainer container = CountyModels.GetCountyArray(searchCounty);
            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// add new county
        /// </summary>
        /// <returns></returns>
        public ActionResult AddCounty()
        {
            string county = Request.Params["data"];
            string messageBack = null;

            CountyModels checkCounty = new CountyModels();
            checkCounty = CountyModels.CheckCounty(county);

            try
            {
                if (county != "")
                {
                    if (checkCounty == null)
                    {
                        CountyModels.AddCounty(county);
                    }
                    else
                    {
                        messageBack = "Judetul  exista deja!";
                    }
                }
				return Json(new { success = true, description = messageBack });
            }
            catch (Exception ex)
            {
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume";
				}
				return Json(new { success = true, description = messageBack });
            }
        }
    }
}
